<?php

namespace App\Http\Controllers\Backend;

use App\Http\Repositories\LinkRepository;
use App\Http\Controllers\Controller;
use App\Http\Requests\Backend\LinkRequest;
use Illuminate\Http\Request;

class LinkController extends Controller
{
    protected $linkRepository;

    public function __construct(LinkRepository $linkRepository)
    {
        $this->linkRepository = $linkRepository;
    }

    /**
     * 获取友情链接列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function index()
    {
        $wait_audit_list = $this->linkRepository->getWaitAudit();
        $link_list = $this->linkRepository->getLinkList();
        return responseResult(compact('wait_audit_list', 'link_list'), self::SUCCESS_CODE);
    }

    /**
     * 审核友链
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function auditLink($id)
    {
        $result = $this->linkRepository->auditLink($id);
        return responseResult([], $result ? self::SUCCESS_CODE : self::ERROR_CODE);
    }

    /**
     * 添加友链
     * @param LinkRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function create(LinkRequest $request)
    {
        $result = $this->linkRepository->edit($request->all());
        return responseResult([], $result ? self::SUCCESS_CODE : self::ERROR_CODE);
    }

    /**
     * 更新友链
     * @param LinkRequest $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function update(LinkRequest $request)
    {
        $result = $this->linkRepository->edit($request->except("id"), $request->get("id"));
        return responseResult([], $result ? self::SUCCESS_CODE : self::ERROR_CODE);
    }

    /**
     * 删除友链
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function delLink($id)
    {
        $result = $this->linkRepository->del($id);
        return responseResult([], $result ? self::SUCCESS_CODE : self::ERROR_CODE);
    }

    /**
     * 启用友链
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function enableLink($id, Request $request)
    {
        $this->validate($request, ['status' => 'required']);
        $result = $this->linkRepository->enableLink($id, $request->get("status"));
        return responseResult([], $result ? self::SUCCESS_CODE : self::ERROR_CODE);
    }
}
